package com.mbengue.corp.artisan.vue.fenetre;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.mbengue.corp.artisan.controleur.Controleur;
import com.mbengue.corp.artisan.utils.Outil;

public class ChoixClient extends JFrame {
  private static final long serialVersionUID = 3068493624881496143L;
  private final JTable tableau;
  private final JButton valider;
  private final JButton annuler;
  private final Controleur controleur;

  public ChoixClient(Controleur controleur) {
    super("Assignation Client à Facture/Devis");
    this.setSize(400, 300);
    this.setLocationRelativeTo(null);

    this.controleur = controleur;
    this.tableau = new JTable();
    this.valider = new JButton("Valider");
    this.annuler = new JButton("Annuler");

    this.initTableau();
    this.ajouterEcouteurs();

    this.getContentPane().add(new JScrollPane(this.tableau), BorderLayout.CENTER);
    JPanel panneau = new JPanel();
    panneau.add(this.valider, BorderLayout.WEST);
    panneau.add(this.annuler, BorderLayout.EAST);
    panneau.setVisible(true);
    this.add(panneau, BorderLayout.SOUTH);

    this.setVisible(true);
  }

  private void initTableau() {
    Object[][] clients = Outil.tableauClient(this.controleur.tousLesClients());
    TableModel model = new DefaultTableModel(clients, Outil.EN_TETE_CLIENT);
    this.tableau.setModel(model);
    this.tableau.repaint();
  }

  private void ajouterEcouteurs() {
    this.annuler.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        dispose();
        setVisible(false);
      }
    });
  }

  public JButton getValider() {
    return valider;
  }

  public JTable getTableau() {
    return tableau;
  }
}
